package com.xxx.common;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.xxx.common.utils.CookieUtil;
import com.xxx.common.utils.DesUtil;
import com.xxx.common.utils.StrUtil;

public class UserAuth {
	
	private static Logger logger = LogManager.getLogger(UserAuth.class.getName());
	
	private static String CookieName = "userAuth";
	
	private static int expiry = 60 * 60 * 2; // 2小时有效
	
	/**
	 * @Title: setUserAuth 
	 * @Description: 设置登录票据
	 * @param userid
	 * @param username
	 * @param response
	 */
	public static void setUserAuth(long userid, String username,HttpServletResponse response) {
		try {
			String userauth =  userid + "|" + username;
			String cookieValue = DesUtil.parseByte2HexStr(userauth.getBytes());
			CookieUtil.addCookie(response, CookieName, cookieValue, "", expiry);
		} catch (Exception e) {
			logger.error("设置登录票据出错:"+e);
		}
	}
	
	/**
	 * @Title: getUserId 
	 * @Description: 从cookie中获取 userid
	 * @param request
	 * @return
	 */
	public static long getUserid(HttpServletRequest request) {
		long userid = Const.DEFAULT_LONG_NULL;
		try {
			Cookie cookies = CookieUtil.getCookieByName(request, CookieName);
			if (cookies != null && StrUtil.notEmpty(cookies.getValue())) {
				String UserAuth = new String(DesUtil.parseHexStr2Byte(cookies.getValue()));
				if (StrUtil.notEmpty(UserAuth)) {
					String[] auth = UserAuth.split("\\|");
					if (auth.length > 0)
						userid = NumberUtils.toLong(auth[0]);
				}
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return userid;
	}
	
	/**
	 * @Title: getUserName 
	 * @Description: 从cookie中获取 username
	 * @param request
	 * @return
	 */
	public static String getUsername(HttpServletRequest request) {
		String username = "";
		try {
			Cookie cookies = CookieUtil.getCookieByName(request, CookieName);
			if (cookies != null && StrUtil.notEmpty(cookies.getValue())) {
				String UserAuth = new String(DesUtil.parseHexStr2Byte(cookies.getValue()));
				if (StrUtil.notEmpty(UserAuth)) {
					String[] auth = UserAuth.split("\\|");
					if (auth.length > 0)
						username = auth[1];
				}
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return username;
	}
	
	/**
	 * @Title: cleanUserAuth 
	 * @Description: 清空登录票据
	 * @param response
	 */
	public static void cleanUserAuth(HttpServletResponse response) {
		int maxAge = 0;
		CookieUtil.addCookie(response, CookieName, "", "", maxAge);
	}
	

	/**
	 * @Title: refreshUserAuth 
	 * @Description: 刷新cookie超时时间
	 * @param oRequest
	 * @param oResponse
	 */
	public static void refreshUserAuth(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Cookie> cookieMap = CookieUtil.ReadCookieMap(request);
		if (cookieMap.containsKey(CookieName)) {
			Cookie cookie = (Cookie) cookieMap.get(CookieName);
			if (cookie != null && StrUtil.notEmpty(cookie.getValue()) ) {
				CookieUtil.addCookie(response, CookieName, cookie.getValue(), "", expiry);
			}

		}
	}
}
